<?php

namespace frontend\models;

use common\helps\tools;
use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use yii\db\Exception;
use yii\db\Query;

/**
 * This is the model class for table "ProjectVideo".
 *
 */
class ProjectVideo extends ActiveRecord
{
    public $type;// 操作类型
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%project_video}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return[
            [['name','video'], 'trim'],
            [['name','video','project_id'], 'required', 'message'=>'{attribute}不能为空'],
            [['id','project_id','type'], 'match', 'pattern' => '/^[1-9][0-9]*$/','message'=>'{attribute}值无效'],
//            [['title'], 'unique', 'message'=>'{attribute}已存在'],
        ];
    }


    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '项目视频id',
            'name' => '视频名称',
            'video'=>'视频源地址',
            'project_id'=>'项目id',
            'type'=>'操作类型',
        ];
    }

    public function scenarios()
    {
        return [
            'default' => [
                'name',
                'video',
                'project_id',
                'created_at',
                'updated_at',
                'deleted_at',
                'type',
            ],
            'pre_update' => [
                'id',
                'type',
            ],

            'create' => [
                'name',
                'video',
                'project_id',
                'created_at',
                'updated_at',
                'deleted_at',
            ],
            'update'=>[
                'id',
                'name',
                'video',
                'project_id',
                'created_at',
                'updated_at',
                'deleted_at',
            ],
        ];
    }



    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }


    /**
     * 获取项目视频列表
     * @param $params
     * @author hey_heyang(635870453@qq.com)
     * @return array
     */
    public function getProjectVideoList($params){
        $condition['project_video.deleted_at'] = null;
        $condition['project_video.project_id'] = intval($params['project_id']);
        $list = (new Query())
            ->select('project_video.*,FROM_UNIXTIME(project_video.created_at) as create_time,admin.username')
            ->from('project_video')
            ->innerJoin('admin','admin.id=project_video.uid')
            ->where($condition)
            ->all();
        if(count($list)){
            foreach($list as &$value){
                $value['username'] = $value['username'].'/'.$value['uid'];
            }
        }
        return empty($list)?array():$list;

    }



}
